﻿using System;
using System.Collections;

namespace CRMCommon
{
    public class CustomEventDataSource
    {
        public CustomEventDataSource(CustomEventList events)
        {
            if (events == null)
                throw new ArgumentNullException("events");
            Events = events;
        }

        public CustomEventDataSource()
            : this(new CustomEventList())
        {
        }

        public CustomEventList Events { get; set; }

        #region ObjectDataSource methods
        
        public void InsertMethodHandler(CustomEvent customEvent)
        {
            Events.Add(customEvent);
        }
        
        public void DeleteMethodHandler(CustomEvent customEvent)
        {
            var eventIndex = Events.GetEventIndex(customEvent.Id);
            if (eventIndex >= 0)
                Events.RemoveAt(eventIndex);
        }
        
        public void UpdateMethodHandler(CustomEvent customEvent)
        {
            var eventIndex = Events.GetEventIndex(customEvent.Id);
            if (eventIndex < 0) return;
            Events.RemoveAt(eventIndex);
            Events.Insert(eventIndex, customEvent);
        }

        public IEnumerable SelectMethodHandler()
        {
            var result = new CustomEventList();
            result.AddRange(Events);
            return result;
        }
        
        #endregion
    }
}
